package com.algorithm.twohundred2threehundred;

import java.util.Arrays;

/**
 * @author tuzhijin
 */
public class MoveZeroes_283 {

    public static void main(String[] args) {
        int[] arrs = {0, 1, 0, 3, 12};
        System.out.println(Arrays.toString(moveZeroes(arrs)));
    }

    public static int[] moveZeroes(int[] nums) {
        int n = nums.length, left = 0, right = 0;
        // {0, 1, 0, 3, 12}->{1, 0, 0, 3, 12}->{1, 3, 0, 0, 12}->{1, 3, 12, 0, 0}
        // 左指针左边全为非0数，左指针右边到右指针左边全为0，右指针右边没数，最后left=3，right=5
        while (right < n) {
            // 遇到非0数就往左边移动
            if (nums[right] != 0) {
                swap(nums, left, right);
                left++;
            }
            right++;
        }
        return nums;
    }

    public static void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }


}
